package cn.fan.web;

import cn.fan.constants.DataBaseConstants;
import cn.fan.pojo.FundFollow;
import cn.fan.service.FundFollowService;
import cn.fan.utils.MyPage;
import cn.fan.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("fundfollows")
public class FundFollowController {
    @Autowired
    private FundFollowService fundFollowService;

    @GetMapping("test")
    public String test(){
        return "hello,test fundfollows!!";
    }

    @GetMapping("add/{fundcode}")
    public R add(@PathVariable("fundcode")String fundcode){
        int i = fundFollowService.add(fundcode);
        return i > DataBaseConstants.NO_DATA ? R.ok("关注成功!") : R.error("不允许重复关注!");
    }

    @GetMapping("delete/{id}")
    public R delete(@PathVariable("id") int id){
        int i = fundFollowService.delete(id);
        return i > DataBaseConstants.NO_DATA ? R.ok("成功取消关注!") : R.error("操作异常!");
    }

    @GetMapping("list")
    public R list(){
        List<FundFollow> list = fundFollowService.list();
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }


    @GetMapping("list/{userId}")
    public R listByUserId(@PathVariable("userId") int userId){
        List<FundFollow> list = fundFollowService.list(userId);
        Map<String,Object> res = new HashMap<>();
        res.put("list",list);
        return R.ok(res);
    }

    @PostMapping("list/{pageSize}/{currentPage}")
    public R page(@RequestBody Map<String,String> map,@PathVariable("pageSize") long pageSize,@PathVariable("currentPage") long currentPage){
        IPage<FundFollow> page = fundFollowService.page(map,currentPage,pageSize);
        MyPage pagination=new MyPage(page);
        Map<String,Object> res = new HashMap<>();
        res.put("pagination",pagination);
        return R.ok(res);
    }

    @PostMapping("list/{userId}/{pageSize}/{currentPage}")
    public R pageByUserId(@RequestBody Map<String,String> map,@PathVariable("userId") int userId,@PathVariable("pageSize") long pageSize,@PathVariable("currentPage") long currentPage){
        IPage<FundFollow> page = fundFollowService.page(map,userId,currentPage,pageSize);
        MyPage pagination=new MyPage(page);
        Map<String,Object> res = new HashMap<>();
        res.put("pagination",pagination);
        return R.ok(res);
    }

    @GetMapping("getById/{id}")
    public FundFollow getById(@PathVariable("id") int id){
        return fundFollowService.getById(id);
    }

//    @PostMapping("simulate")
//    public R simulate(@RequestBody Map<String,String> simMap){
//
//    }
}
